using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.InventoryquantityManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using Microsoft.VisualBasic;

namespace Business.InventoryquantityManagement
{
    [Authorize(BusinessPermissions.Inventoryquantity.Default)]
    public class InventoryquantityAppService : ApplicationService,IInventoryquantityAppService
    {
        private const string FormName = "Inventoryquantity";
        private IRepository<Inventoryquantity, Guid> _repository;
        private IRepository<WarehouseLocation, Guid> _WarehouseLocation;
        private IRepository<WarehouseArea, Guid> _WarehouseArea;

        public InventoryquantityAppService(IRepository<Inventoryquantity, Guid> repository, IRepository<WarehouseLocation, Guid> warehouseLocation, IRepository<WarehouseArea, Guid> warehouseArea)
        {
            _repository = repository;
            _WarehouseLocation = warehouseLocation;
            _WarehouseArea = warehouseArea;
        }




        
        #region 增删改查基础方法

        public async Task<InventoryquantityDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Inventoryquantity, InventoryquantityDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<InventoryquantityDto>> GetAll(GetInventoryquantityInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync());
            var inventoryquantity = await _repository.GetListAsync();
            var Location = await _WarehouseLocation.GetListAsync();
            var wareArea = await _WarehouseArea.GetListAsync();
            var query = (from a in inventoryquantity
                         join b in Location on a.Code equals b.Code
                         join c in wareArea on b.PCode.ToUpper() equals c.Id.ToString().ToUpper()
                         select new InventoryquantityDto
                        {
                            Inventoryquantityavailable = a.Inventoryquantityavailable,
                            Inventoryquantityfrozen = a.Inventoryquantityfrozen,
                            Inventoryquantitylocking = a.Inventoryquantitylocking,
                            Investoryquantityactual = a.Investoryquantityactual,
                             Code = b.Code,
                             UsePattern = b.UsePattern,
                             Name = c.Name,
                             CreationTime =a.CreationTime,

                         }).AsQueryable();
            //query = query.WhereIf(input.CreationTime!=null,x=>x.CreationTime == input.CreationTime );
            if (input.StartDate != null && input.EndDate != null)
            {
                query = query.Where(x => x.CreationTime >= input.StartDate && x.CreationTime <= input.EndDate);
            }
            if (!string.IsNullOrEmpty(input.Code))
            { 
                query = query.Where(x => x.Code.Contains(input.Code));
            }
            var totalCount =  query.Count();
            var items =  query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount==0? input.SkipCount : 0)
                       .Take(input.MaxResultCount)
                       .ToList();

            //var dto = ObjectMapper.Map<List<Inventoryquantity>, List<InventoryquantityDto>>(items);
            var dto= new PagedResultDto<InventoryquantityDto>(totalCount,items);
            return dto;
        }

        public async Task<InventoryquantityDto> DataPost(CreateOrUpdateInventoryquantityDto input)
        {
            Inventoryquantity result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateInventoryquantityDto, Inventoryquantity>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Inventoryquantity, InventoryquantityDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

     
        #endregion

    }
}